package ch03;

import java.util.ArrayList;
import java.util.List;

//顺序查找（基于无序链表）
public class SequentialSearchST<Key, Value> {

   private class Node{
        Key key;
        Value value;
        Node next;

       public Node(Key key, Value value, Node next) {
           this.key = key;
           this.value = value;
           this.next = next;
       }
   }

    private Node first;

    public Value get(Key key) {
        for (Node i = first; i != null; i = i.next) {
            if (i.key.equals(key)) {
                return i.value;
            }
        }
        return null;
    }

    public int put(Key key, Value value) {
        for (Node i = first; i != null; i = i.next) {
            if (i.key.equals(key)) {
                i.value = value;
                return 0;
            }
        }
        first = new Node(key, value, first);
        return 1;
    }

    public String values() {
        boolean flag = false;
        StringBuilder sb = new StringBuilder();
        for (Node i = first; i != null; i = i.next) {
            flag = true;
            sb.append("key: " + i.key + " value: " + i.value).append("\n");
        }
        return flag ? sb.toString() : "";

    }

}
